﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Zoo.Infrastructure.Data.Core;
using Zoo.Cat.Domain.Entities.Jobs;
using Zoo.Infrastructure.Caching;
using Zoo.Infrastructure.Events;
using Zoo.Cat.Domain.Unity;
using Zoo.Cat.Domain.Entities.Lovs;

namespace Zoo.Cat.Application.Jobs
{
    public class JobService : IJobService
    {
        #region Fields
        private readonly IRepository<Job> _jobRepository;
        private readonly ICacheManager _cacheManager;
        private readonly IEventPublisher _eventPublisher;
        #endregion

        #region Ctor
        public JobService(IRepository<Job> jobRepository,
            ICacheManager cacheManager, IEventPublisher eventPublisher)
        {
            this._cacheManager = cacheManager;
            this._eventPublisher = eventPublisher;
            this._jobRepository = jobRepository;
        }
        #endregion

        #region Method

        public IQueryable<Job> FindJobList(String jobName)
        {
            var list = from item in _jobRepository.Table
                       where item.JobTitle.Contains(jobName)
                       select item;
            return list;
        }
        #endregion

        public IEnumerable<int> GetAssociatedLovIdList(IEnumerable<Job> jobList, LovType lovType)
        {
            IEnumerable<int> idList = null;
            switch (lovType)
            {
                case LovType.Category:
                    idList = jobList.Select(item => item.Category);
                    break;
                case LovType.CompanySize:
                    idList = jobList.Select(item => item.CompanySize);
                    break;
                case LovType.YearsOfExperience:
                    idList = jobList.Where(item => item.YearsOfExperience.HasValue).Select(item => item.YearsOfExperience.Value);
                    break;
                case LovType.EducationLevel:
                    idList = jobList.Where(item => item.EducationLevel.HasValue).Select(item => item.EducationLevel.Value);
                    break;
                //case LovType.:
                //    idList = jobList.Select(item => item.SalaryRange);
                //    break;
                case LovType.JobType:
                    idList = jobList.Where(item => item.JobType.HasValue).Select(item => item.JobType.Value);
                    break;
                case LovType.CompanyType:
                    idList = jobList.Select(item => item.CompanyType);
                    break;
            }
            idList = idList.Distinct();
            return idList;
        }
    }
}
